package jdbc.study.mapper;

import jdbc.study.domain.JDBCInfo;
import jdbc.study.domain.Movie;
import jdbc.study.domain.Page;
import jdbc.study.utils.DataSourceUtils;

import java.beans.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 影片信息操作类
 */
public class MovieMapper {


    public List<Movie> getMovies(Page page, JDBCInfo jdbcInfo) throws SQLException {
        List<Movie> movies = new ArrayList<>();
        DataSourceUtils dataSourceUtils = new DataSourceUtils();
        dataSourceUtils.initSimpleConnection(jdbcInfo.getUrl(), jdbcInfo.getUser(), jdbcInfo.getPassword());
        String sql = "select * from movies_metadata where deleted = 1 limit ?, ?";
        int index = (page.getNo()-1)*page.getSize();
        ResultSet resultSet = dataSourceUtils.doSQL(sql, index, page.getSize());
        while(resultSet.next()) {
            Movie movie = new Movie();
            movie.setId(resultSet.getInt("id"));
            movie.setBudget(resultSet.getInt("budget"));
            movie.setGenres(resultSet.getString("genres"));
            movie.setHomepage(resultSet.getString("homepage"));
            movie.setOriginal_language(resultSet.getString("original_language"));
            movie.setOriginal_title(resultSet.getString("original_title"));
            movie.setPopularity(resultSet.getString("popularity"));
            movie.setRelease_date(resultSet.getString("release_date"));
            movie.setRevenue(resultSet.getDouble("revenue"));
            movie.setStatus(resultSet.getString("status"));
            movie.setDeleted(resultSet.getInt("deleted"));
            movies.add(movie);
        }

        return movies;
    }

    public List<Movie> getMoviesAdvanced(Page page) throws SQLException, IntrospectionException, InvocationTargetException, IllegalAccessException, InstantiationException, NoSuchMethodException {
        List<Movie> movies = new ArrayList<>();
        DataSourceUtils dataSourceUtils = new DataSourceUtils();
        dataSourceUtils.initSimpleConnection("jdbc:mysql://101.35.43.117/lifeDB", "root", "hias2023");
        String sql = "select * from movies_metadata where deleted = 0 limit ?, ?";
        int index = (page.getNo()-1)*page.getSize();
        ResultSet resultSet = dataSourceUtils.doSQL(sql, index, page.getSize());
        ResultSetMetaData metaData = resultSet.getMetaData();
        int columnCount = metaData.getColumnCount();
        Class targetClass = Movie.class;
        while(resultSet.next()) {
            Movie movie = (Movie) targetClass.getConstructor(null).newInstance();
            for(int i = 1; i <= columnCount; i++) {
                String columnLabel = metaData.getColumnLabel(i);
                Object columnValue = resultSet.getObject(columnLabel);

                PropertyDescriptor propertyDescriptor = new PropertyDescriptor(columnLabel, targetClass);
                Method method = propertyDescriptor.getWriteMethod();
                method.invoke(movie, columnValue);
            }
            System.out.println(movie.toString());
        }

        return movies;
    }
}
